//
//  ElephantWebViewController.m
//  MyProject
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ElephantWebViewController.h"
#import "DataBase.h"
#import "ButifulArticle.h"
#import "ElephantS.h"
#import "MBProgressHUD.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDKUI.h>

@interface ElephantWebViewController ()<UIWebViewDelegate,UIScrollViewDelegate,MBProgressHUDDelegate>
@property (nonatomic,strong) UIWebView *web;
@property (nonatomic, strong) UIView *menuView;
@property (nonatomic, strong) UIButton *shanreBtn;
@property (nonatomic, strong) UIButton *collectBtn;
@property (nonatomic,assign)BOOL collection;
@property (nonatomic,strong) NSMutableArray *array;
@property (nonatomic,strong) UIView *myview;


@end

@implementation ElephantWebViewController
- (void)viewWillAppear:(BOOL)animated
{
       self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    
        if (![[NSUserDefaults standardUserDefaults]boolForKey:@"isWhite"]) {
            self.myview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100000)];
            [self.web.scrollView addSubview:_myview];
            _myview.alpha = 0.5;
            _myview.backgroundColor = [UIColor grayColor];
            _myview.userInteractionEnabled = YES;
        }
        else {
            self.myview.hidden = YES;
        }
  
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    NSLog(@"####%@",self.newid);
//    NSLog(@"ERTYU%@",self.url);
    self.web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height + 49)];
    NSString *str = self.url;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
    [self.view addSubview:self.web];
    [self.web loadRequest:request];
    
    //    隐藏滑动条
    self.web.scrollView.showsVerticalScrollIndicator = NO;
 
    //web转scrollView
    self.web.scrollView.delegate = self;
    
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(20,  20, 40, 40);
    backButton.layer.cornerRadius = 20;
    backButton.clipsToBounds = YES;
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    backButton.tag = 1;
    UIImageView *imagev = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui-2@2x.png"]];
    imagev.center = CGPointMake(20, 20);
    [backButton addSubview:imagev];
    
//    UIButton *zkbutton = [UIButton buttonWithType:UIButtonTypeSystem];
//    zkbutton.frame = CGRectMake(self.view.bounds.size.width - 60, 20, 40, 40);
//    zkbutton.layer.cornerRadius = 20;
//    zkbutton.clipsToBounds = YES;
//    [zkbutton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:zkbutton];
//    zkbutton.tag = 2;
//    zkbutton.backgroundColor = [UIColor whiteColor];
//    UIImageView *imagev2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iconfont-dianjishou@2x.png"]];
//    [zkbutton addSubview:imagev2];
    
    
    
    self.shanreBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _shanreBtn.frame = CGRectMake(self.view.bounds.size.width - 60, 20, 40, 40);
    _shanreBtn.layer.cornerRadius = 20;
    _shanreBtn.clipsToBounds = YES;
    [_shanreBtn addTarget:self action:@selector(clickAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_shanreBtn];
    _shanreBtn.backgroundColor = [UIColor clearColor];
    _shanreBtn.hidden = YES;
    [_shanreBtn setImage:[[UIImage imageNamed:@"iconfont-icon84"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
//    UIImageView *imagev3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iconfont-dianjishou@2x.png"]];
//    [zkbutton addSubview:imagev3];
    
    self.collectBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _collectBtn.frame = CGRectMake(self.view.bounds.size.width - 60, 20, 40, 40);
    _collectBtn.layer.cornerRadius = 20;
    _collectBtn.clipsToBounds = YES;
    [_collectBtn addTarget:self action:@selector(clickAction2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_collectBtn];
    _collectBtn.backgroundColor = [UIColor clearColor];
    _collectBtn.hidden = YES;
    [_collectBtn setImage:[[UIImage imageNamed:@"iconfont-xin"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
//    UIImageView *imagev4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iconfont-dianjishou@2x.png"]];
//    [zkbutton addSubview:imagev4];
    UIButton *zkbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    zkbutton.frame = CGRectMake(self.view.bounds.size.width - 60, 20, 40, 40);
    zkbutton.layer.cornerRadius = 20;
    zkbutton.clipsToBounds = YES;
    [zkbutton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:zkbutton];
    zkbutton.tag = 2;
    [zkbutton setImage:[[UIImage imageNamed:@"iconfont-dianjishou"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
}
- (void)clickAction:(UIButton *)btn
{
//    NSLog(@"ftru");
//    UIButton *sharebutton = (UIButton *)[self.view viewWithTag:3];
//    UIButton *collectionbutton = (UIButton *)[self.view viewWithTag:4];
//    if (sharebutton) {
//        [UIView animateWithDuration:0.4 animations:^{
//            sharebutton.frame = CGRectMake(self.view.bounds.size.width - 60, 70, 40, 40);
//        }];
//    }
//    if (collectionbutton) {
//        [UIView animateWithDuration:0.5 animations:^{
//            collectionbutton.frame = CGRectMake(self.view.bounds.size.width - 60, 120, 40, 40);
//        }];
//    }
    if (_shanreBtn.frame.origin.y == 20) {
        NSLog(@"lalal");
        _shanreBtn.hidden = NO;
        _collectBtn.hidden = NO;
        [UIView animateWithDuration:0.5 animations:^{
            _shanreBtn.frame = CGRectMake(self.view.bounds.size.width - 60, 70, 40, 40);
            _collectBtn.frame = CGRectMake(self.view.bounds.size.width - 60, 120, 40, 40);
        }];
        
    }else {
        [UIView animateWithDuration:0.5 animations:^{
            
            _shanreBtn.frame = CGRectMake(self.view.bounds.size.width - 60, 20, 40, 40);
            _collectBtn.frame = CGRectMake(self.view.bounds.size.width - 60, 20, 40, 40);
        } completion:^(BOOL finished) {
            
            _shanreBtn.hidden = YES;
            _collectBtn.hidden = YES;
        }];
    }
  
    
    
}
- (void)clickAction1:(UIButton *)btn1
{
    // 1.创建分享参数
    NSMutableDictionary *shareParames = [NSMutableDictionary dictionary];
    
    if (self.url != nil) {
        
        [shareParames SSDKSetupShareParamsByText:self.needtitle images:@[self.headpic] url:[NSURL URLWithString:self.url] title:@"分享" type:SSDKContentTypeImage];
    }
    
    //进行分享
    [ShareSDK showShareActionSheet:btn1 items:nil
                       shareParams:shareParames
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   switch (state) {
                       case SSDKResponseStateBegin:
                       {
                           NSLog(@"开始分享");
                       }
                           break;
                       case SSDKResponseStateSuccess:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                       }
                           break;
                       case SSDKResponseStateFail:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                       }
                           break;
                       case SSDKResponseStateCancel:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享取消"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                       }
                           break;
                       default:
                           break;
                   }
               }];


    
}
- (void)clickAction2:(UIButton *)btn2
{
   
    _collection = !_collection;
    
    if (_collection == YES) {
           [_collectBtn setImage:[[UIImage imageNamed:@"iconfont-xin-2@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
      
           
        DataBase *coreData = [DataBase dataBase];
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"ButifulArticle"];
        NSArray *arr = [coreData.managedObjectContext executeFetchRequest:request error:nil];
        self.array = [NSMutableArray array];
        for (ButifulArticle *ele in arr) {
            [_array addObject:ele.url];
        }
        if (![_array containsObject:self.url]) {
            if (self.url) {
                
           
            ButifulArticle *ele = [NSEntityDescription insertNewObjectForEntityForName:@"ButifulArticle" inManagedObjectContext:coreData.managedObjectContext];
            ele.title = self.needtitle;
            ele.url = self.url;
            ele.name = self.headpic;
            [coreData saveContext];
            
        }
        
        
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertView animated:YES completion:^{
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertView addAction:action];
        }
        
    }
    else{
        [_collectBtn setImage:[[UIImage imageNamed:@"iconfont-xin@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
         DataBase *coreData = [DataBase dataBase];
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"ButifulArticle"];
//        //条件查询(谓词)
//        request.predicate = [NSPredicate predicateWithFormat:@"%@",self.title];
//        //把数据装到数组里
        NSArray *arr1 =  [coreData.managedObjectContext  executeFetchRequest:request error:nil];
        NSLog(@"%@",arr1);
        for (ButifulArticle *ele in arr1) {
            if ( [ele.url isEqualToString:self. url]) {
                //删除数据
                [coreData.managedObjectContext deleteObject:ele];
                [coreData saveContext];
                NSLog(@"%@",NSHomeDirectory());
                
            }
        }
        

        
        
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"已取消收藏" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertView animated:YES completion:^{
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertView addAction:action];
        
    }
    
}
- (void)backAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    UIButton *backButton = (UIButton *)[self.view viewWithTag:1];
    if (backButton) {
        [UIView animateWithDuration:0.4 animations:^{
            backButton.frame = CGRectMake(20,  -40, 40, 40);
        }];
    
    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UIButton *backButton = (UIButton *)[self.view viewWithTag:1];
    if (backButton) {
                [UIView animateWithDuration:0.5 animations:^{
        backButton.frame = CGRectMake(20,  20, 40, 40);
                }];
    }

}
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
//{
//    UIButton *backButton = (UIButton *)[self.view viewWithTag:1];
//    if (backButton) {
////        [UIView animateWithDuration:0.5 animations:^{
//            backButton.frame = CGRectMake(20,  20, 40, 40);
////        }];
//    }
//
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
